package itbour.onetouchshow.activity.myorder;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.adapter.BaseRecyclerHolder;
import com.andview.refreshview.XRefreshView;
import com.google.gson.Gson;
import com.ys.uilibrary.swip.SwipeMenuLayout;

import java.util.ArrayList;
import java.util.List;

import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.preview.verticalpreview.verticalmodel.VerticalmodelActivity;
import itbour.onetouchshow.base.BaseRefreshActivity;
import itbour.onetouchshow.base.BaseRefreshAdapter;
import itbour.onetouchshow.bean.DesignListBean;
import itbour.onetouchshow.bean.MyCollectBean;
import itbour.onetouchshow.bean.page.Page;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.constants.platform.DataPlatform;
import itbour.onetouchshow.utils.ImageLoader;
import itbour.onetouchshow.utils.JumpUtils;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.widget.RoundImageView;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MyCollectActivity extends BaseRefreshActivity<MyCollectContract.View, MyCollectPresenter> implements MyCollectContract.View {
    List<MyCollectBean.ListBean> mdata = new ArrayList<>();
    Page page;

    @Override
    public void loadSucceed(String result) {
        showContentView();
        showXRefreshViewNetWorkSuccessView();
        page.finishLoad(true);
        MyCollectBean myCollectBean = new Gson().fromJson(result, MyCollectBean.class);
        List<MyCollectBean.ListBean> data = myCollectBean.getList();
        if (pullStatus == ON_LOAD) {
            mdata.addAll(data);
            xRefreshView.stopLoadMore();
        } else if (pullStatus == ON_REFRESH) {
            mdata = data;
            xRefreshView.stopRefresh();
        }
        if (myCollectBean != null && mPageIndex == myCollectBean.getTotalPage() - 1) {
            xRefreshView.setLoadComplete(true);
        } else {
            xRefreshView.setLoadComplete(false);
        }
        baseRefreshAdapter.notifyDataSetChanged(mdata);
    }

    @Override
    public void loadFaild(String error) {
        page.finishLoad(false);
        //showXRefreshViewNetWorkErrorView();
        showEmptyView("暂无收藏", R.mipmap.icon_no_collect);
    }


    @Override
    protected void initTitle() {
        super.initTitle();
        setTitleText("我的收藏");
    }

    @Override
    public BaseRefreshAdapter getBaseRefreshAdapter() {
        return new OrderAdapter(getApplicationContext(), mdata, R.layout.item_mycollect_adapter);
    }

    @Override
    public View getHeadView() {
        return null;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        onLoadData();
    }


    private void onLoadData() {
        page = new Page() {
            @Override
            public void load(int param1, int param2) {
                L_.e("load" + param1);
                mPageIndex = param1;
                mPresenter.loadMyCollectListV1_3(MyCollectActivity.this);
                //showLoadingView();
            }

        };
        pullStatus = ON_REFRESH;
        if (mPresenter != null) {
            page.loadPage(true);
        }
    }


    @Override
    protected void initView() {
        super.initView();
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                pullStatus = ON_REFRESH;
                if (mPresenter != null) {
                    page.loadPage(true);
                }
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                UIUtils.getHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pullStatus = ON_LOAD;
                        if (mPresenter != null) {
                            page.loadPage(false);
                        }
                    }

                }, AppConst.DELAYED_TIME);

            }
        });
    }

    @Override
    public void cancleCollectSuccess(MyCollectBean.ListBean listBean) {
        if (mdata.size() == 1) {
            mdata.clear();
            pullStatus = ON_REFRESH;
            if (mPresenter != null) {
                page.loadPage(true);
            }
        } else {
            mdata.remove(listBean);
            baseRefreshAdapter.notifyDataSetChanged(mdata);
            if (mdata.size() == 0) {
                showEmptyView();
            } else {
                showContentView();
            }
        }

    }


    public class OrderAdapter extends BaseRefreshAdapter<MyCollectBean.ListBean> {

        public OrderAdapter(Context context, List<MyCollectBean.ListBean> datas, int itemLayoutId) {
            super(context, datas, itemLayoutId);
        }

        @Override
        protected void convert(BaseRecyclerHolder var1, MyCollectBean.ListBean var2, int var3) {
            SwipeMenuLayout swipeMenuLayout = var1.getView(R.id.id_root_swp);
            TextView tvTime = var1.getView(R.id.id_tv_time);
            TextView tvName = var1.getView(R.id.id_tv_name);
            TextView tvType = var1.getView(R.id.id_tv_type);
            RoundImageView imageView = var1.getView(R.id.id_ig_av);
            LinearLayout btnDelete = var1.getView(R.id.btnDelete_ly);
            btnDelete.setOnClickListener(v -> {
                swipeMenuLayout.quickClose();
                mPresenter.cancalCollect(var2);
                //T_.showToastReal("取消收藏");
            });

            switch (var2.getOpType()) {
                case 1:
                    tvType.setText("印品");
                    break;
                case 2:
                    tvType.setText("视频");
                    break;
                case 3:
                    tvType.setText("PPT");
                    break;
                default:
                    break;
            }
            ImageLoader.getInstance().showImage(MyCollectActivity.this, var2.getThumb(), imageView.getImageView());
            tvName.setText(var2.getName());
            tvTime.setText(var2.getTime());
            var1.getView(R.id.id_root_ly).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int opType = var2.getOpType();
                    switch (opType) {
                        case DataPlatform.ORTYPE_VERTICAL:
                            int postion = mdata.indexOf(var2);
                            Intent itToVertical = new Intent(getApplication(), VerticalmodelActivity.class);

                            List<DesignListBean.ListBean> list = new ArrayList<>();
                            DesignListBean.ListBean dBean = new DesignListBean.ListBean();
                            dBean.setId(var2.getId());
                            dBean.setAuthor(var2.getAuthor());
                            dBean.setName(var2.getName());
                            dBean.setOpType(var2.getOpType());
                            dBean.setThumb(var2.getThumb());
                            dBean.setH(var2.getH());
                            dBean.setW(var2.getW());
                            list.add(dBean);
                            itToVertical.putExtra("pageContentList", new Gson().toJson(list));
                            itToVertical.putExtra("postion", postion);
                            startActivity(itToVertical);
                            break;
                        case DataPlatform.ORTYPE_VIDIO:
//                            Bundle bundle = new Bundle();
//                            bundle.putString(AppConst.DOCID, var2.getId() + "");
//                            bundle.putString(AppConst.MOUDLE, AppConst.W0RK);
                            // Bundle bundle = IntentCode.getBundle(var2.getId(), WorkPlatform.WOEK);
                            // openActivity(VideoplayActivity1.class, bundle);

                            JumpUtils.goToVideoPlayer(MyCollectActivity.this, var2.getId());
//                            Bundle bundle = IntentCode.getBundle(var2.getId(), WorkPlatform.MODULE);
//                            openActivity(VideoplayActivity1.class, bundle);
                            break;
                        case DataPlatform.ORTYPE_PPT:
                            break;
                        default:
                            break;
                    }
                }
            });
        }
    }
}
